package com.senlingspace.web.controller.weixin;

import com.alibaba.fastjson2.JSON;
import com.senlingspace.common.core.domain.AjaxResult;
import com.senlingspace.framework.security.context.SecurityUtilExt;
import com.senlingspace.system.domain.vo.MiniAppCartUpdateVo;
import com.senlingspace.system.domain.vo.MiniAppCartVo;
import com.senlingspace.system.domain.vo.MiniAppConfirmOrderRequest;
import com.senlingspace.system.service.ISlkjShoppingCartService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 小程序购物车相关接口
 *
 * @author lebronjames
 * @date 2024/10/03
 */
@Validated
@AllArgsConstructor
@Slf4j
@RestController
@RequestMapping(path = "/wmp/cart/v1")
public class MiniAppShoppingCartController {

    private final ISlkjShoppingCartService iSlkjShoppingCartService;


    /**
     * 添加购物车
     *
     * @param request
     * @return {@link AjaxResult }
     */
    @PostMapping(path = "/addCart")
    public AjaxResult addCart(@Valid @RequestBody MiniAppCartVo request) {
        log.info("小程序添加购物车，入参：{}", JSON.toJSONString(request));
        return iSlkjShoppingCartService.addCart(request, SecurityUtilExt.getCustomerId(), SecurityUtilExt.getShopId());
    }

    /**
     * 购物车列表
     *
     * @return {@link AjaxResult }
     */
    @GetMapping(path = "/list")
    public AjaxResult list() {
        log.info("小程序购物车列表，入参：{}", SecurityUtilExt.getCustomerId());
        return AjaxResult.success(iSlkjShoppingCartService.findByCustomerId(SecurityUtilExt.getCustomerId()));
    }

    /**
     * 更新购物车
     *
     * @param request
     * @return {@link AjaxResult }
     */
    @PutMapping(path = "/updateCart")
    public AjaxResult updateCart(@RequestBody MiniAppCartUpdateVo request) {
        log.info("小程序购物车更新，入参：{}", JSON.toJSONString(request));
        return iSlkjShoppingCartService.updateCart(request, SecurityUtilExt.getCustomerId(), SecurityUtilExt.getShopId());
    }

    /**
     * 确认下单（目前只支持商品）
     * <p>
     * 商品详情页立即购买、购物车页面去结算
     *
     * @param request
     * @return {@link AjaxResult }
     */
    @PostMapping(path = "/confirmOrder")
    public AjaxResult confirmOrder(@RequestBody MiniAppConfirmOrderRequest request) {
        log.info("小程序购物车确认下单，入参：{}", JSON.toJSONString(request));
        return iSlkjShoppingCartService.confirmOrder(request, SecurityUtilExt.getCustomerId(), SecurityUtilExt.getShopId(),SecurityUtilExt.getMobile());
    }

}
